package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mapper.ArticleMapper;
import com.example.mapper.ArticleTagMapper;
import com.example.mapper.TagMapper;
import com.example.pojo.Article;
import com.example.pojo.ArticleTag;
import com.example.pojo.Tag;
import com.example.service.ArticleService;
import com.example.service.TagService;
import com.example.vo.ArticleVo;
import com.example.vo.Result;
import com.example.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleTagMapper articleTagMapper;


    //查询标签的图标显示在文章上
    @Override
    public List<TagVo> findTagsByArticleId(Long articleId) {
        //mybatisplus 不能进行多表查询 所以我们使用配置文件进行连表查询
         List<Tag> tags= tagMapper.findTagsByArticleId(articleId);
        return copyList(tags);
    }

    //最热标签
    @Override
    public Result hots(int limit) {
        /**
         * 1.标签所拥有的文章数量最多就是最热标签
         * 2.查询，根据tag_id分组计数，从大到小排列，取前limit个
         * 3.取完之后就 将所有的id和名字取出
         */
        //查询最热的前几个
        List<Long> tagIds= tagMapper.findHotsTagIds(limit);

        //因为sql语句后面不能为空，所以提前判断是否为空，为空就json返回空数据
        if(CollectionUtils.isEmpty(tagIds)){
            return Result.success(Collections.emptyList());
        }

        //需要的是tagid和tagName的tag对象
        List<Tag> tagList= tagMapper.findTagsTagIds(tagIds);

        return Result.success(tagList);
    }

    @Override
    public Result findAll() {

        LambdaQueryWrapper<Tag>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(Tag::getId,Tag::getTagName);
        List<Tag> tags = tagMapper.selectList(queryWrapper);
        return Result.success(copyList(tags));
    }

    @Override
    public Result findAllDetail() {
        List<Tag> tags = tagMapper.selectList(null);
        return Result.success(copyList(tags));
    }

    @Override
    public Result findDetailById(Long id) {
        Tag tag = tagMapper.selectById(id);
        return Result.success(copy(tag));
    }


    private List<ArticleVo> copyListArtileVO(List<Article> articleList) {
        List<ArticleVo> articleVoList = new ArrayList<>();
        for (Article article : articleList) {
            articleVoList.add(copyArticle(article));
        }
        return articleVoList;
    }

    private ArticleVo copyArticle(Article article) {
        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article,articleVo);
        return articleVo;
    }


    public List<TagVo> copyList(List<Tag> tagList){
        List<TagVo> tagVoList=new ArrayList<>();
        for (Tag tag : tagList) {
            tagVoList.add(copy(tag));
        }
        return tagVoList;
    }

    public TagVo copy(Tag tag)
    {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        return tagVo;
    }


}
